package dwei.core.remote
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.Operation;
	import mx.rpc.remoting.RemoteObject;

	[Event(name="error", type="flash.events.Event")]
	[Event(name="complete", type="flash.events.Event")]
	public class RemoteProxy extends EventDispatcher
	{
//============================================================================
//STATIC
//============================================================================
		public static function getInstance():RemoteProxy
		{
			return new RemoteProxy(new RemoteProxyInner());
		}
//============================================================================
//PROPERTY
//============================================================================
		protected var _rm:RemoteObject;
		protected var _isRequesting:Boolean;
		protected var _data:*;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function RemoteProxy(inner:RemoteProxyInner)
		{
			_rm = new RemoteObject();
			_rm.addEventListener(ResultEvent.RESULT, result_handler);
			_rm.addEventListener(FaultEvent.FAULT, fault_handler);
			_isRequesting = false;
		}
//============================================================================
//PUBLIC
//============================================================================
		public function send(funcName:String, parameters:Array, destination:String, endpoint:String):void
		{
			if (isRequesting) return;
			_rm.destination = destination;
			_rm.endpoint = endpoint;
			var o:Operation = _rm.getOperation(funcName) as Operation;
			o.argumentNames = [];
			o.arguments = {};
			var len:int = parameters.length;
			for (var index:int = 0;index < len;index++)
			{
				o.argumentNames.push(index.toString());
				o.arguments[index.toString()] = parameters[index];
			}
			o.send();
		}
//============================================================================
//HANDLER
//============================================================================
		private function result_handler(event:ResultEvent):void
		{
			_data = event.result;
			_isRequesting = false;
			if (hasEventListener("complete")) dispatchEvent(new Event("complete"));
		}
		private function fault_handler(event:FaultEvent):void
		{
			_data = event.fault.toString();
			_isRequesting = false;
			if (hasEventListener("error")) dispatchEvent(new Event("error"));
		}
//============================================================================
//GETTER/SETTER
//============================================================================
		/**
		 * 是否正在请求服务器
		 */		
		public function get isRequesting():Boolean
		{
			return _isRequesting;
		}

		/**
		 * 请求结果实例。请求成功时，为服务器返回的数据；请求出错时，为出错信息。
		 */
		public function get data():*
		{
			return _data;
		}


	}
}
class RemoteProxyInner
{
	
}